# ============================================================
# ANALYSIS SCRIPT — Neuroinflamación y Trastornos Neurodegenerativos
# Autor: de la Serna, Juan Moises (UNIR)
# ORCID: 0000-0002-8401-8018
# Versión: 2.0 | Fecha: 2026-03-19
# DOI dataset: 10.7910/DVN/HWJO4Y
# ============================================================

library(ggplot2)
library(dplyr)
library(tidyr)
library(readr)
library(scales)
library(RColorBrewer)

# ── Carga de datos ──────────────────────────────────────────
datos <- read_csv("Neuroinflamacion_Trastornos_Neurodegenerativos_BaseDatos.tab",
                  locale = locale(encoding = "UTF-8"))

# Paleta de colores por trastorno
colores_trastorno <- c(
  "EA" = "#2166AC",   # Azul oscuro — Alzheimer
  "EP" = "#D6604D",   # Rojo — Parkinson
  "EM" = "#4DAC26"    # Verde — Esclerosis Múltiple
)

# ── FIGURA 1: Niveles de citocinas neuroinflamatorias ──────
fig1_data <- datos %>%
  filter(category == "Biomarcador_LCR",
         variable_name %in% c("IL-1beta", "IL-6", "TNF-alpha", "IFN-gamma")) %>%
  pivot_longer(cols = c(value_patients, value_controls),
               names_to = "grupo", values_to = "nivel") %>%
  mutate(grupo = ifelse(grupo == "value_patients", "Pacientes", "Controles"))

fig1 <- ggplot(fig1_data, aes(x = variable_name, y = nivel, fill = grupo)) +
  geom_bar(stat = "identity", position = "dodge", color = "white", linewidth = 0.3) +
  facet_wrap(~disorder, labeller = labeller(disorder = c(EA="Alzheimer", EP="Parkinson", EM="Esclerosis Múltiple"))) +
  scale_fill_manual(values = c("Pacientes" = "#2166AC", "Controles" = "#B2D8F7")) +
  labs(title = "Citocinas Neuroinflamatorias en LCR por Trastorno",
       subtitle = "Niveles medios (pg/mL) — Pacientes vs. Controles",
       x = "Citocina", y = "Concentración (pg/mL)", fill = NULL,
       caption = "Fuente: de la Serna (2026). DOI: 10.7910/DVN/HWJO4Y") +
  theme_minimal(base_size = 12) +
  theme(legend.position = "top", strip.text = element_text(face = "bold"),
        plot.title = element_text(face = "bold"), panel.grid.minor = element_blank())

ggsave("fig1_citocinas_LCR.png", fig1, width = 12, height = 6, dpi = 300, bg = "white")

# ── FIGURA 2: Biomarcadores específicos EA (Amiloide, Tau, P-Tau) ──
fig2_data <- datos %>%
  filter(disorder == "EA", category == "Biomarcador_LCR",
         variable_name %in% c("Beta-amiloide42", "Tau_total", "Fosfo-tau181")) %>%
  pivot_longer(cols = c(value_patients, value_controls),
               names_to = "grupo", values_to = "nivel") %>%
  mutate(grupo = ifelse(grupo == "value_patients", "Alzheimer", "Controles"),
         variable_label = case_when(
           variable_name == "Beta-amiloide42" ~ "Aβ42 (pg/mL)",
           variable_name == "Tau_total" ~ "Tau total (pg/mL)",
           variable_name == "Fosfo-tau181" ~ "P-Tau181 (pg/mL)"
         ))

fig2 <- ggplot(fig2_data, aes(x = variable_label, y = nivel, fill = grupo)) +
  geom_bar(stat = "identity", position = "dodge", color = "white") +
  scale_fill_manual(values = c("Alzheimer" = "#2166AC", "Controles" = "#B2D8F7")) +
  labs(title = "Biomarcadores Diagnósticos del Alzheimer en LCR",
       subtitle = "Beta-amiloide 42, Tau total y Fosfo-tau181",
       x = NULL, y = "Concentración (pg/mL)", fill = NULL,
       caption = "Fuente: de la Serna (2026). DOI: 10.7910/DVN/HWJO4Y") +
  theme_minimal(base_size = 12) +
  theme(legend.position = "top", plot.title = element_text(face = "bold"))

ggsave("fig2_biomarkers_EA.png", fig2, width = 8, height = 6, dpi = 300, bg = "white")

# ── FIGURA 3: Progresión clínica comparada ──────────────────
fig3_data <- datos %>%
  filter(category == "Progresion_Clinica") %>%
  mutate(escala = case_when(
    disorder == "EA" ~ "CDR (0–3)",
    disorder == "EP" ~ "UPDRS-III (0–108)",
    disorder == "EM" ~ "EDSS (0–10)"
  ))

fig3 <- ggplot(fig3_data, aes(x = disorder, y = value_patients, fill = disorder)) +
  geom_bar(stat = "summary", fun = "mean", color = "white") +
  geom_errorbar(stat = "summary", fun.data = "mean_se", width = 0.2) +
  scale_fill_manual(values = colores_trastorno) +
  scale_x_discrete(labels = c("EA" = "Alzheimer\n(CDR)", "EP" = "Parkinson\n(UPDRS-III)", "EM" = "Esclerosis M.\n(EDSS)")) +
  labs(title = "Puntuación Media en Escalas de Progresión Clínica",
       subtitle = "Media ± error estándar por trastorno",
       x = "Trastorno (Escala)", y = "Puntuación media", fill = NULL,
       caption = "Fuente: de la Serna (2026). DOI: 10.7910/DVN/HWJO4Y") +
  theme_minimal(base_size = 12) +
  theme(legend.position = "none", plot.title = element_text(face = "bold"))

ggsave("fig3_progresion_clinica.png", fig3, width = 8, height = 6, dpi = 300, bg = "white")

# ── FIGURA 4: Factores de riesgo genético (OR) ──────────────
fig4_data <- datos %>%
  filter(category == "Genetica") %>%
  mutate(variable_label = paste0(variable_name, "\n(", disorder, ")"))

fig4 <- ggplot(fig4_data, aes(x = reorder(variable_label, value_patients), y = value_patients, fill = disorder)) +
  geom_bar(stat = "identity", color = "white") +
  geom_hline(yintercept = 1, linetype = "dashed", color = "gray40") +
  coord_flip() +
  scale_fill_manual(values = colores_trastorno, labels = c("EA"="Alzheimer","EP"="Parkinson","EM"="Esclerosis M.")) +
  labs(title = "Factores de Riesgo Genético — Odds Ratios",
       subtitle = "OR respecto a población general (línea discontinua = OR=1)",
       x = NULL, y = "Odds Ratio", fill = "Trastorno",
       caption = "Fuente: de la Serna (2026). DOI: 10.7910/DVN/HWJO4Y") +
  theme_minimal(base_size = 12) +
  theme(legend.position = "right", plot.title = element_text(face = "bold"))

ggsave("fig4_riesgo_genetico.png", fig4, width = 10, height = 6, dpi = 300, bg = "white")

# ── FIGURA 5: Carga económica global ────────────────────────
fig5_data <- datos %>%
  filter(category == "Carga_Economica") %>%
  mutate(disorder_label = case_when(
    disorder == "EA" ~ "Alzheimer y\nDemencias",
    disorder == "EP" ~ "Parkinson",
    disorder == "EM" ~ "Esclerosis\nMúltiple"
  ))

fig5 <- ggplot(fig5_data, aes(x = disorder_label, y = value_patients / 1000, fill = disorder)) +
  geom_bar(stat = "identity", color = "white", width = 0.6) +
  scale_fill_manual(values = colores_trastorno) +
  scale_y_continuous(labels = scales::comma_format(suffix = " B")) +
  labs(title = "Carga Económica Mundial de Trastornos Neurodegenerativos",
       subtitle = "Coste anual global estimado (miles de millones USD)",
       x = NULL, y = "Miles de millones USD/año", fill = NULL,
       caption = "Fuente: de la Serna (2026). DOI: 10.7910/DVN/HWJO4Y") +
  theme_minimal(base_size = 12) +
  theme(legend.position = "none", plot.title = element_text(face = "bold"))

ggsave("fig5_carga_economica.png", fig5, width = 8, height = 6, dpi = 300, bg = "white")

cat("\n✅ Análisis completado. 5 figuras guardadas.\n")
